home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 16 / CU Amiga Magazine's Super CD-ROM 16 (1997-10-16)(EMAP Images)(GB)[!][issue 1997-11].iso / CUCD / Graphics / Ghostscript / source / zmath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-08  |  5.8 KB  |  261 lines

  1. /* Copyright (C) 1989, 1992, 1993, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zmath.c */
  20. /* Mathematical operators */
  21. #include "math_.h"
  22. #include "ghost.h"
  23. #include "gxfarith.h"
  24. #include "errors.h"
  25. #include "oper.h"
  26. #include "store.h"
  27.  
  28. /*
  29.  * Define the current state of random number generator.  We have to
  30.  * implement this ourselves because the Unix rand doesn't provide anything
  31.  * equivalent to rrand.  Note that the value always lies in the range
  32.  * [0..0x7ffffffe], even if longs are longer than 32 bits.
  33.  *
  34.  * The state must be public so that context switching can save and
  35.  * restore it.  (Even though the Red Book doesn't mention this,
  36.  * we verified with Adobe that this is the case.)
  37.  */
  38. long zrand_state;
  39.  
  40. /* Initialize the random number generator. */
  41. void
  42. zrand_state_init(long *pstate)
  43. {    *pstate = 1;
  44. }
  45. private void
  46. zmath_init(void)
  47. {    zrand_state_init(&zrand_state);
  48. }
  49.  
  50. /****** NOTE: none of these operators currently ******/
  51. /****** check for floating over- or underflow.    ******/
  52.  
  53. /* <num> sqrt <real> */
  54. private int
  55. zsqrt(register os_ptr op)
  56. {    double num;
  57.     int code = real_param(op, &num);
  58.  
  59.     if ( code < 0 )
  60.       return code;
  61.     if ( num < 0.0 )
  62.       return_error(e_rangecheck);
  63.     make_real(op, sqrt(num));
  64.     return 0;
  65. }
  66.  
  67. /* <num> arccos <real> */
  68. private int
  69. zarccos(register os_ptr op)
  70. {    double num, result;
  71.     int code = real_param(op, &num);
  72.  
  73.     if ( code < 0 )
  74.       return code;
  75.     result = acos(num) * radians_to_degrees;
  76.     make_real(op, result);
  77.     return 0;
  78. }
  79.  
  80. /* <num> arcsin <real> */
  81. private int
  82. zarcsin(register os_ptr op)
  83. {    double num, result;
  84.     int code = real_param(op, &num);
  85.  
  86.     if ( code < 0 )
  87.       return code;
  88.     result = asin(num) * radians_to_degrees;
  89.     make_real(op, result);
  90.     return 0;
  91. }
  92.  
  93. /* <num> <denom> atan <real> */
  94. private int
  95. zatan(register os_ptr op)
  96. {    double args[2];
  97.     double result;
  98.     int code = num_params(op, 2, args);
  99.  
  100.     if ( code < 0 ) return code;
  101.     if ( args[0] == 0 )        /* on X-axis, special case */
  102.        {    if ( args[1] == 0 )
  103.           return_error(e_undefinedresult);
  104.         result = (args[1] < 0 ? 180 : 0);
  105.        }
  106.     else
  107.        {    result = atan2(args[0], args[1]) * radians_to_degrees;
  108.         if ( result < 0 )
  109.           result += 360;
  110.        }
  111.     make_real(op - 1, result);
  112.     pop(1);
  113.     return 0;
  114. }
  115.  
  116. /* <num> cos <real> */
  117. private int
  118. zcos(register os_ptr op)
  119. {    double angle;
  120.     int code = real_param(op, &angle);
  121.  
  122.     if ( code < 0 )
  123.       return code;
  124.     make_real(op, gs_cos_degrees(angle));
  125.     return 0;
  126. }
  127.  
  128. /* <num> sin <real> */
  129. private int
  130. zsin(register os_ptr op)
  131. {    double angle;
  132.     int code = real_param(op, &angle);
  133.  
  134.     if ( code < 0 )
  135.       return code;
  136.     make_real(op, gs_sin_degrees(angle));
  137.     return 0;
  138. }
  139.  
  140. /* <base> <exponent> exp <real> */
  141. private int
  142. zexp(register os_ptr op)
  143. {    double args[2];
  144.     double result;
  145.     double ipart;
  146.     int code = num_params(op, 2, args);
  147.  
  148.     if ( code < 0 ) return code;
  149.     if ( args[0] == 0.0 && args[1] == 0.0 )
  150.       return_error(e_undefinedresult);
  151.     if ( args[0] < 0.0 && modf(args[1], &ipart) != 0.0 )
  152.       return_error(e_undefinedresult);
  153.     result = pow(args[0], args[1]);
  154.     make_real(op - 1, result);
  155.     pop(1);
  156.     return 0;
  157. }
  158.  
  159. /* <posnum> ln <real> */
  160. private int
  161. zln(register os_ptr op)
  162. {    double num;
  163.     int code = real_param(op, &num);
  164.  
  165.     if ( code < 0 )
  166.       return code;
  167.     if ( num <= 0.0 )
  168.       return_error(e_rangecheck);
  169.     make_real(op, log(num));
  170.     return 0;
  171. }
  172.  
  173. /* <posnum> log <real> */
  174. private int
  175. zlog(register os_ptr op)
  176. {    double num;
  177.     int code = real_param(op, &num);
  178.  
  179.     if ( code < 0 )
  180.       return code;
  181.     if ( num <= 0.0 )
  182.       return_error(e_rangecheck);
  183.     make_real(op, log10(num));
  184.     return 0;
  185. }
  186.  
  187. /* - rand <int> */
  188. private int
  189. zrand(register os_ptr op)
  190. {    /*
  191.      * We use an algorithm from CACM 31 no. 10, pp. 1192-1201,
  192.      * October 1988.  According to a posting by Ed Taft on
  193.      * comp.lang.postscript, Level 2 (Adobe) PostScript interpreters
  194.      * use this algorithm too:
  195.      *    x[n+1] = (16807 * x[n]) mod (2^31 - 1)
  196.      */
  197. #define A 16807
  198. #define M 0x7fffffff
  199. #define Q 127773            /* M / A */
  200. #define R 2836                /* M % A */
  201.     zrand_state = A * (zrand_state % Q) - R * (zrand_state / Q);
  202.     /* Note that zrand_state cannot be 0 here. */
  203.     if ( zrand_state <= 0 )
  204.       zrand_state += M;
  205. #undef A
  206. #undef M
  207. #undef Q
  208. #undef R
  209.     push(1);
  210.     make_int(op, zrand_state);
  211.     return 0;
  212. }
  213.  
  214. /* <int> srand - */
  215. private int
  216. zsrand(register os_ptr op)
  217. {    long state;
  218.     check_type(*op, t_integer);
  219.     state = op->value.intval;
  220. #if arch_sizeof_long > 4
  221.     /* Trim the state back to 32 bits. */
  222.     state = (int)state;
  223. #endif
  224.     /*
  225.      * The following somewhat bizarre adjustments are according to
  226.      * public information from Adobe describing their implementation.
  227.      */
  228.     if ( state < 1 )
  229.       state = -(state % 0x7ffffffe) + 1;
  230.     else if ( state > 0x7ffffffe )
  231.       state = 0x7ffffffe;
  232.     zrand_state = state;
  233.     pop(1);
  234.     return 0;
  235. }
  236.  
  237. /* - rrand <int> */
  238. private int
  239. zrrand(register os_ptr op)
  240. {    push(1);
  241.     make_int(op, zrand_state);
  242.     return 0;
  243. }
  244.  
  245. /* ------ Initialization procedure ------ */
  246.  
  247. BEGIN_OP_DEFS(zmath_op_defs) {
  248.     {"1arccos", zarccos},        /* extension */
  249.     {"1arcsin", zarcsin},        /* extension */
  250.     {"2atan", zatan},
  251.     {"1cos", zcos},
  252.     {"2exp", zexp},
  253.     {"1ln", zln},
  254.     {"1log", zlog},
  255.     {"0rand", zrand},
  256.     {"0rrand", zrrand},
  257.     {"1sin", zsin},
  258.     {"1sqrt", zsqrt},
  259.     {"1srand", zsrand},
  260. END_OP_DEFS(zmath_init) }
  261.